package 反转链表;

public class s2 {
    public static void main(String[] args) {
        ListNode head = null;
        for (int i = 5; i >= 1; i--) {
            if(head == null){
                head = new ListNode(i);
            }
            else{
                head = new ListNode(i,head);
            }
        }

        printOut(head);


        ListNode p = head;
        ListNode c = null;
        ListNode n = null;
        ListNode r = null;
        if(p.next != null){
            c = p.next;
            r = c;
            p.next = null;
        }
        while(c.next != null){
            n = c.next;
            c.next = p;
            p = c;
            c = n;
            r = c;
        }

        r.next = p;


        printOut(r);
    }


    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val,  ListNode next) { this.val = val; this.next = next; }
    }

    public static void   printOut(ListNode head) {
        System.out.println(head.val);
        while(head.next != null){
            head = head.next;
            System.out.println(head.val);
        }
    }
}
